

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.exfile &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex/" />
    <link rel="search" title="Search" href="../../../search/" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../">Module code</a> &raquo;</li>
        
      <li>cup.exfile</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.exfile</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    file related functions</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">fcntl</span>

<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">err</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">platforms</span>

<span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">():</span>
    <span class="kn">import</span> <span class="nn">tempfile</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;LockFile&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_SHARED&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_EXCLUSIVE&#39;</span><span class="p">,</span>
    <span class="s1">&#39;FILELOCK_NONBLOCKING&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_UNLOCK&#39;</span><span class="p">,</span> <span class="s1">&#39;TempFile&#39;</span>
<span class="p">]</span>


<span class="n">FILELOCK_EXCLUSIVE</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_EX</span>
<span class="n">FILELOCK_SHARED</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_SH</span>
<span class="n">FILELOCK_NONBLOCKING</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_NB</span>
<span class="n">FILELOCK_UNLOCK</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_UN</span>


<div class="viewcode-block" id="LockFile"><a class="viewcode-back" href="../../../cup/#cup.exfile.LockFile">[docs]</a><span class="k">class</span> <span class="nc">LockFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    lock file class</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fpath</span><span class="p">,</span> <span class="n">locktype</span><span class="o">=</span><span class="n">FILELOCK_EXCLUSIVE</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        exclusive lockfile, by default.</span>

<span class="sd">        Notice that the file CANNOT exist before you intialize a LockFile obj.</span>
<span class="sd">        Otherwise, it will raise cup.err.LockFileError</span>

<span class="sd">        :raise:</span>
<span class="sd">            cup.err.LockFileError if we encounter errors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span> <span class="o">=</span> <span class="n">fpath</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span> <span class="o">=</span> <span class="n">locktype</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># if FILELOCK_EXCLUSIVE == locktype:</span>
            <span class="c1">#     self._fhandle = os.open(</span>
            <span class="c1">#         self._fpath, os.O_CREAT|os.O_EXCL|os.O_RDWR</span>
            <span class="c1">#     )</span>
            <span class="c1"># else:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_CREAT</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDWR</span>
            <span class="p">)</span>
        <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span>
                <span class="s1">&#39;catch unkown error type:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;del the instance&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">)</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;failed to close lockfile:</span><span class="si">{0}</span><span class="s1">, msg:</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>

<div class="viewcode-block" id="LockFile.lock"><a class="viewcode-back" href="../../../cup/#cup.exfile.LockFile.lock">[docs]</a>    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needposix</span>
    <span class="k">def</span> <span class="nf">lock</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">blocking</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        lock the file</span>

<span class="sd">        :param blocking:</span>
<span class="sd">            If blocking is True, will block there until cup gets the lock.</span>
<span class="sd">            True by default.</span>

<span class="sd">        :return:</span>
<span class="sd">            return False if locking fails</span>

<span class="sd">        :raise Exception:</span>
<span class="sd">            raise cup.err.LockFileError if blocking is False and</span>
<span class="sd">            the lock action failed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">flags</span> <span class="o">=</span> <span class="mh">0x1</span>
        <span class="k">if</span> <span class="n">FILELOCK_SHARED</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span><span class="p">:</span>
            <span class="n">flags</span> <span class="o">=</span> <span class="n">FILELOCK_SHARED</span>
        <span class="k">elif</span> <span class="n">FILELOCK_EXCLUSIVE</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span><span class="p">:</span>
            <span class="n">flags</span> <span class="o">=</span> <span class="n">FILELOCK_EXCLUSIVE</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="s1">&#39;does not support this lock type&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">blocking</span><span class="p">:</span>
            <span class="n">flags</span> <span class="o">|=</span> <span class="n">FILELOCK_NONBLOCKING</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">flock</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ret</span></div>

<div class="viewcode-block" id="LockFile.unlock"><a class="viewcode-back" href="../../../cup/#cup.exfile.LockFile.unlock">[docs]</a>    <span class="k">def</span> <span class="nf">unlock</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;unlock the locked file&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">fcntl</span><span class="o">.</span><span class="n">flock</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">,</span> <span class="n">FILELOCK_UNLOCK</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="TempFile"><a class="viewcode-back" href="../../../cup/#cup.exfile.TempFile">[docs]</a><span class="k">class</span> <span class="nc">TempFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    tempfile, the temp file will be deleted immediately after the lifetime.</span>

<span class="sd">    You can use TempFile like the original Python File Object.</span>

<span class="sd">    ::</span>

<span class="sd">        tmp = TempFile(&#39;./&#39;)</span>
<span class="sd">        tmp.write / read /seek / etc</span>
<span class="sd">        tmp.close()</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filedir</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">suffix</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param filedir:</span>
<span class="sd">            temp file dir which contains the temp file</span>

<span class="sd">        :prefix:</span>
<span class="sd">            prefix of the temp filename</span>

<span class="sd">        :suffix:</span>
<span class="sd">            suffix of the file name. e.g. &#39;.tmp&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fdir</span> <span class="o">=</span> <span class="n">filedir</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fp</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span> <span class="o">=</span> <span class="n">prefix</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_suffix</span> <span class="o">=</span> <span class="n">suffix</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;enter&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fp</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryFile</span><span class="p">(</span>
                <span class="nb">dir</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_fdir</span><span class="p">,</span>
                <span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="p">,</span>
                <span class="n">suffix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_suffix</span>
            <span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fp</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">NotImplementedYet</span><span class="p">(</span><span class="s1">&#39;TemporaryFile&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_val</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        exit</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>